home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’92 / NetWarmer / source / ScrollText.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-11  |  4.0 KB  |  179 lines  |  [TEXT/KAHL]

  1. /* © 1988, Bowers Development Corp. */
  2. /* ScrollText.c */
  3.  
  4. #include "Globals.h"
  5. #include "Cursors.h"
  6. #include "Scrolling.h"    
  7.  
  8. #include "ScrollText.h"    
  9.  
  10. #define textMargin    4                                 
  11.  
  12. /*----------*/
  13. void SetMaxScroll    ()
  14. {
  15.     #define active        0
  16.     #define inactive    255
  17.  
  18.     short            textSize;
  19.     short            pageSize;
  20.  
  21.     if (cur->vScroll != nil) {
  22.         pageSize = GetCRefCon (cur->vScroll) + 1;
  23.         if (cur->text != nil) {
  24.             textSize = (**(cur->text)).nLines;
  25.         } else {
  26.             textSize = 0;
  27.         }
  28.         if (textSize > pageSize) {
  29.             SetCtlMax (cur->vScroll, textSize - pageSize);
  30.             HiliteControl (cur->vScroll, active);
  31.         } else {
  32.             SetCtlMax (cur->vScroll, 0);
  33.             HiliteControl (cur->vScroll, inactive);
  34.         }
  35.     }
  36. } /*SetMaxScroll*/
  37.  
  38. /*----------*/
  39. void ScrollToBar    ()
  40. {
  41.     short            curScroll;
  42.     short            newScroll;
  43.     register TEPtr        curTE;
  44.  
  45.     if (cur->vScroll != nil) {
  46.         if (cur->text != nil) {
  47.             HLock ((Handle) (cur->text));
  48.             curTE = *(cur->text);
  49.             curScroll = curTE->viewRect.top - curTE->destRect.top;
  50.             newScroll = GetCtlValue (cur->vScroll) * curTE->lineHeight;
  51.             if (newScroll != curScroll) {
  52.                 if (curTE->selStart == curTE->selEnd) {
  53.                     TEDeactivate (cur->text);
  54.                 }
  55.                 TEScroll (0, (curScroll - newScroll), cur->text);    
  56.                 if (curTE->selStart == curTE->selEnd) {
  57.                     TEActivate (cur->text);
  58.                 }
  59.             }
  60.             HUnlock ((Handle) (cur->text));
  61.         }
  62.     }
  63. } /*ScrollToBar*/
  64.  
  65. /*----------*/
  66. void SetBarToChar    (short        charNr,
  67.                      Boolean    toMiddle);
  68. void SetBarToChar    (charNr,
  69.                      toMiddle)
  70. short        charNr;
  71. Boolean        toMiddle;
  72. {
  73.     short            lineNr;
  74.     short            pageSize;
  75.     register TEPtr        curTE;
  76.  
  77.     if (cur->vScroll != nil) {
  78.         curTE = *(cur->text);
  79.         lineNr = 0;
  80.         while (curTE->lineStarts [lineNr + 1] <= charNr) {
  81.             lineNr++;
  82.         }
  83.         if (toMiddle) {
  84.             pageSize = GetCRefCon (cur->vScroll) + 1;
  85.             lineNr = lineNr - (pageSize / 2);
  86.         }
  87.         SetCtlValue (cur->vScroll, lineNr);
  88.     }
  89. } /*SetBarToChar*/
  90.  
  91. /*----------*/
  92. void ScrollToSelection    ()
  93. {
  94.     short            topLine;
  95.     short            bottomLine;
  96.     short            pageSize;
  97.     register TEPtr        curTE;
  98.  
  99.     if (cur->vScroll != nil) {
  100.         HLock ((Handle) (cur->text));
  101.         curTE = *(cur->text);
  102.         if (GetCtlMax (cur->vScroll) > 0) {
  103.             topLine     = GetCtlValue (cur->vScroll);
  104.             pageSize    = GetCRefCon (cur->vScroll) + 1;
  105.             bottomLine  = topLine + pageSize - 1;
  106.             if ((curTE->selStart < curTE->lineStarts [topLine])
  107.             ||  (curTE->selStart > curTE->lineStarts [bottomLine])) {
  108.                 SetBarToChar (curTE->selStart, true);
  109.             }
  110.         }
  111.         ScrollToBar ();
  112.         HUnlock ((Handle) (cur->text));
  113.     }
  114. } /*ScrollToSelection*/
  115.  
  116. /*----------*/
  117. void ResizeText        ()
  118. {
  119.     short            topLine;
  120.     short            topChar;
  121.     short            pageSize;
  122.     register TEPtr        curTE;
  123.  
  124.     InvalRect (&(curWindow->portRect));
  125.     if (cur->text != nil) {
  126.         SetCursor (&(**watch));
  127.         HLock ((Handle) (cur->text));
  128.         curTE = *(cur->text);
  129.         curTE->viewRect = curWindow->portRect;
  130.         if (cur->vScroll != nil) {
  131.             curTE->viewRect.right  = curTE->viewRect.right  + 1 - sBarWidth;
  132.         }
  133.         if (cur->hScroll != nil) {
  134.             curTE->viewRect.bottom = curTE->viewRect.bottom + 1 - sBarWidth;
  135.         }
  136.         curTE->destRect = curTE->viewRect;
  137.         InsetRect (&(curTE->destRect), textMargin, 0);
  138.         TECalText (cur->text);
  139.  
  140.         if (cur->vScroll != nil) {
  141.             pageSize = (curTE->viewRect.bottom
  142.                       - curTE->viewRect.top) / curTE->lineHeight;
  143.             SetCRefCon (cur->vScroll, pageSize - 1);
  144.             topLine    = GetCtlValue (cur->vScroll);
  145.             topChar    = curTE->lineStarts [topLine];
  146.             SetMaxScroll ();
  147.             SetBarToChar (topChar, false);
  148.             ScrollToBar ();
  149.         }
  150.         HUnlock ((Handle) (cur->text));
  151.     }
  152. } /*ResizeText*/
  153.  
  154. /*----------*/
  155. pascal Boolean AutoScroll    ()
  156. {
  157.     Point            mousePoint;
  158.     Rect            viewRect;
  159.     RgnHandle        saveClip;
  160.  
  161.     if (cur->vScroll != nil) {
  162.         saveClip = NewRgn ();
  163.         GetClip  (saveClip);
  164.         ClipRect (&(curWindow->portRect));
  165.         GetMouse (&mousePoint);
  166.         viewRect = (**(cur->text)).viewRect;
  167.         if (mousePoint.v < viewRect.top) {
  168.             DoScrollPart (cur->vScroll, inUpButton);
  169.             ScrollToBar ();
  170.         } else if (mousePoint.v > viewRect.bottom) {
  171.             DoScrollPart (cur->vScroll, inDownButton);
  172.             ScrollToBar ();
  173.         }
  174.         SetClip    (saveClip);
  175.         DisposeRgn (saveClip);
  176.     }
  177.     return (true);
  178. } /*AutoScroll*/
  179.